package com.itheima.es.controller;

import com.itheima.es.service.HotelService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.websocket.server.PathParam;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/hotel")
@CrossOrigin
public class HotelController {

    @Autowired
    private HotelService hotelService;


    //批量导入
    @PostMapping("/addAll")
    public int addAll() {
        return hotelService.addDocToES();
    }

    //查询所有
    @GetMapping("/list")
    public Map<String, Object> queryHotelList() {
        Map<String, Object> map = hotelService.matchAllQuery();
        return this.getJsonMap(true, "", map);
    }

    //分页查询
    @GetMapping("/page/list")
    public Map<String, Object> page(@PathParam("current") Integer current, @PathParam("pageSize") Integer pageSize) {
        Map<String, Object> map = hotelService.pageQuery(current, pageSize);
        return this.getJsonMap(true, "", map);
    }

    //根据品牌精确查询
    @GetMapping("/searchTermQuery")
    public Map<String, Object> searchTermQuery(@RequestParam Map<String, Object> searchParam, @PathParam("current") Integer current, @PathParam("pageSize") Integer pageSize) {
        Map<String, Object> map = hotelService.brandTermQuery(current, pageSize, searchParam);
        return this.getJsonMap(true, "", map);
    }

    //根据酒店名称分词查询
    @GetMapping("/searchMatchQuery")
    public Map<String, Object> searchMatchQuery(@RequestParam Map<String, Object> searchParam, @PathParam("current") Integer current, @PathParam("pageSize") Integer pageSize) {
        Map<String, Object> map = hotelService.nameMatchQuery(current, pageSize, searchParam);
        return this.getJsonMap(true, "", map);
    }

    //根据酒店品牌模糊查询
    @GetMapping("/searchWildcardQuery")
    public Map<String, Object> searchWildcardQuery(@RequestParam Map<String, Object> searchParam, @PathParam("current") Integer current, @PathParam("pageSize") Integer pageSize) {
        Map<String, Object> map = hotelService.nameWildcardQuery(current, pageSize, searchParam);
        return this.getJsonMap(true, "", map);
    }

    //根据name,synopsis,area,address进行多域查询
    @GetMapping("/searchQueryStringQuery")
    public Map<String, Object> searchQueryStringQuery(@RequestParam Map<String, Object> searchParam, @PathParam("current") Integer current, @PathParam("pageSize") Integer pageSize) {
        Map<String, Object> map = hotelService.searchQueryStringQuery(current, pageSize, searchParam);
        return this.getJsonMap(true, "", map);
    }

    //搜索框自动提示
    @GetMapping("/searchSuggestInfo")
    public Map<String, Object> searchSuggestInfo(@RequestParam("key") String key) {
        return getJsonMap(true, "", hotelService.searchSuggestInfo(key));
    }

    //根据销量排序查询
    @GetMapping("/searchSortQuery")
    public Map<String, Object> searchSortQuery(@RequestParam Map<String, Object> searchParam, @PathParam("current") Integer current, @PathParam("pageSize") Integer pageSize) {
        Map<String, Object> map = hotelService.salesSortQuery(current, pageSize, searchParam);
        return this.getJsonMap(true, "", map);
    }

    //根据价格范围查询
    @GetMapping("/searchRangeQuery")
    public Map<String, Object> searchRangeQuery(@RequestParam Map<String, Object> searchParam, @PathParam("current") Integer current, @PathParam("pageSize") Integer pageSize) {
        Map<String, Object> map = hotelService.priceRangeQuery(current, pageSize, searchParam);
        return this.getJsonMap(true, "", map);
    }


    //多条件查询
    @GetMapping("/searchBoolQuery")
    public Map<String, Object> searchBoolQuery(@RequestParam Map<String, Object> searchParam, @PathParam("current") Integer current, @PathParam("pageSize") Integer pageSize) {
        Map<String, Object> map = hotelService.searchBoolQuery(current, pageSize, searchParam);
        return this.getJsonMap(true, "", map);
    }

    //自动纠错查询
    @GetMapping("/searchFuzzyQuery")
    public Map<String, Object> searchFuzzyQuery(@RequestParam Map<String, Object> searchParam, @PathParam("current") Integer current, @PathParam("pageSize") Integer pageSize) {
        Map<String, Object> map = hotelService.searchFuzzyQuery(current, pageSize, searchParam);
        return this.getJsonMap(true, "", map);
    }

    //高亮查询
    @GetMapping("/searchHighLight")
    public Map<String, Object> searchHighLight(@RequestParam Map<String, Object> searchParam, @PathParam("current") Integer current, @PathParam("pageSize") Integer pageSize) {
        Map<String, Object> map = hotelService.searchHighLight(current, pageSize, searchParam);
        return this.getJsonMap(true, "", map);
    }





    //品牌分组聚合
    @GetMapping("/searchBrandGroupQuery")
    public Map<String, Object> searchBrandGroupQuery(@RequestParam Map<String, Object> searchParam, @PathParam("current") Integer current, @PathParam("pageSize") Integer pageSize) {
        Map<String, Object> result = hotelService.searchBrandGroupQuery(current, pageSize, searchParam);
        return this.getJsonMap(true, "", result);
    }

     //自定义日期时间段聚合统计某品牌的酒店销量
    @GetMapping("/searchDateHistogram")
    public Map<String, Object> searchDateHistogram(@RequestParam Map<String, Object> searchParam) {
        List<Map<String, Object>> result = hotelService.searchDateHistogram(searchParam);
        return this.getJsonMap(true, "", result);
    }
	
	//权重查询
    @GetMapping("/searchScoreQuery")
    public Map<String, Object> searchScoreQuery(@RequestParam Map<String, Object> searchParam, @PathParam("current") Integer current, @PathParam("pageSize") Integer pageSize) {
        Map<String, Object> map = hotelService.searchScoreQuery(current, pageSize, searchParam);
        return this.getJsonMap(true, "", map);
    }


    //同统一结果处理
    public Map<String, Object> getJsonMap(boolean status, String message, Object entity) {
        Map<String, Object> json = new HashMap(3);
        json.put("status", status);
        json.put("message", message);
        json.put("entity", entity);
        return json;
    }


}
